package net.funtip.ormgenerator.mojo;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import net.funtip.ormgenerator.generator.ClassGenerator;
import net.funtip.ormgenerator.model.Model;
import net.funtip.ormgenerator.parser.ModelParser;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;

/**
 * @goal generate
 * @phase generate-sources
 * @requiresDependencyResolution compile
 * @author Perry Prust
 * @phase process-sources
 */
public class OrmGeneratorMojo extends AbstractMojo {
	/**
	 * Location of generated files.
	 * @parameter expression="${project.build.directory}"
	 * @required
	 */
	private String outputDirectory;
	
	/**
	 * Location of the model file.
	 * @parameter expression="${project.build.directory}"
	 * @required
	 */
	private String modelFile;
	
	/**
	 * @parameter 
	 */
	private boolean generateJpa;

	/**
	 * @parameter 
	 */
	private boolean generateDao;

	/**
	 * @parameter 
	 */
	private boolean generateObjectify;
	

	public void execute() throws MojoExecutionException {
		Model model = getModel();
		ClassGenerator cg = new ClassGenerator(generateJpa, generateObjectify, generateDao);
		getLog().info("generating class files for model: "+model+" to directory "+outputDirectory);
		cg.generateClassFiles(outputDirectory, model);

	}
	
	private Model getModel() {
		ModelParser parser = new ModelParser();
		try {
			parser.parse(new FileInputStream(modelFile));
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		return parser.getModel();
	}
}
